import unittest
import requests
from api.api_shortlink_create import ApiShortLink
from api.api_register_login import ApiRegisterLogin
from parameterized import parameterized

from util import read_json


class TestShortLinkCreate(unittest.TestCase):
    #初始化
    def setUp(self) -> None:
        self.session = requests.session()
        self.shortlink = ApiShortLink(self.session)
        ApiRegisterLogin(self.session).api_login("aaa","12345678")

    #结束
    def tearDown(self) -> None:
        self.session.close()

    #测试方法：单个短链接创建方法
    @parameterized.expand(read_json("create_shortlink.json","create_shortlink_test_cases"))
    def test_create_shortlink(self,domain,originUrl,gid,createdType,validDateType,validDate,describe,expect_result):
        try:
            res = self.shortlink.create_shortlink(domain, originUrl, gid, createdType, validDateType, validDate,describe)
            res_json = res.json()  # 解析响应为JSON
            print(res_json)

            if expect_result == "短链接创建成功":
                self.assertTrue(res_json.get("success"),
                                msg=f"正向用例失败，预期success=True，实际返回: {res_json}")
            else:
                self.assertFalse(res_json.get("success"),
                                 msg=f"逆向用例失败，预期success=False，实际返回: {res_json}")
                self.assertIn(expect_result, res_json.get("message", ""),
                              msg=f"错误信息不匹配，预期包含: {expect_result}, 实际返回: {res_json.get('message')}")
        except Exception as e:
            raise
